package com.va.yahooweather.net;

import com.va.yahooweather.commons.units.TempratureUnit;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.logging.Level;
import org.apache.log4j.Logger;

public class YahooRetriever {

    private static final Logger log = Logger.getLogger(YahooRetriever.class);
    
    public static final String WEATHER_SERVICE_BASE_URL = "http://weather.yahooapis.com/forecastrss";
	public static final String WOEID_PARAMETER_NAME = "w";
	public static final String DEGREES_PARAMETER_NAME = "u";

    public String retrieve(String zipcode){
        return retrieve(zipcode, TempratureUnit.C);
    }
    
    public String retrieve(String zipcode, TempratureUnit unit){
        try {
            String url = composeUrl(zipcode, unit);
            
            log.info("open url " + url);
            URLConnection conn = new URL(url).openConnection();
            InputStream  in = conn.getInputStream();
            
            return copy(in, zipcode + ".xml");
            
        } catch (IOException ex)
        {
            java.util.logging.Logger.getLogger(YahooRetriever.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        return "";
    }
    
    private String composeUrl(String woeid)
	{
		return composeUrl(woeid, TempratureUnit.C);
	}
    
    private String composeUrl(String woeid, TempratureUnit unit)
	{
		StringBuilder url = new StringBuilder(WEATHER_SERVICE_BASE_URL);
		url.append('?').append(WOEID_PARAMETER_NAME).append('=').append(woeid);
		url.append('&').append(DEGREES_PARAMETER_NAME).append('=').append(unit.toString().toLowerCase());
		
		return url.toString();		
	}

    private String copy(InputStream in, String filename)
    {
        String path = "";
        FileOutputStream out = null;
        try
        {
            log.info("touch new file");
            File f = new File(filename);
            out = new FileOutputStream(f);
            byte[] buffer = new byte[1024];
            int len;
            while ((len = in.read(buffer)) != -1) {
                out.write(buffer, 0, len);
            }
            log.info("copied all data to file");
            
            log.info("closing writer");
            out.close();
            
            log.info("closing input stream");
            in.close();
            
            path = f.getAbsolutePath();
            log.info("successful, actual path is \""+ path + "\"");
        } catch (FileNotFoundException ex)
        {
            log.info("can not open file to write: FileNotFoundException");
        } catch (IOException ex)
        {
            log.info("can not open file to write: IOException");
        } finally
        {
            try
            {
                out.close();
            } catch (IOException ex)
            {
                log.info("can not close file stream: IOException");
            }
        }
        
        
        return path;
    }
}
